<template>
	<view>

		<view class="popupn">
		  <view class="title line1" @click="changeStoreList">
		    <image class="mer_logo" :src="storeInfo.mer_avatar" mode=""></image>
		    {{storeInfo.mer_name}}
		    <text v-if="!downStatus" class="iconfont icon-xiala1 spin"></text>
		    <text v-else class="iconfont icon-xiala1"></text>
		  </view>
		</view>
		<shopList ref="shopList" @changeStoreClose="changeStoreClose" @getStoreInfo="getStoreInfo" :id='mer_id'></shopList>
		

		<view class='top-box'>
			<view class='header'>
				<view class='picTxt acea-row row-between-wrapper' style="justify-content: center;">
					<view class='text'>
						<view>总共{{totalPonds}}个塘，{{totalPartitions}}个分区，{{totalSeats}}个钓位，使用设备{{totalDevices}}台</view>
					</view>
				</view>
			</view>
			<view class='filter acea-row row-around'>
				<view class='item' :class='filterStatus==-1 ? "on": ""' @click="clickFilter(-1)">
					<view>全部</view>
					<view class='num'>{{totalSeats}}</view>
				</view>
				<view class='item' :class='filterStatus==0 ? "on": ""' @click="clickFilter(0)">
					<view>使用中</view>
					<view class='num'>{{totalUsing}}</view>
				</view>
				<view class='item' :class='filterStatus==1 ? "on": ""' @click="clickFilter(1)">
					<view>空闲</view>
					<view class='num'>{{totalFree}}</view>
				</view>
				<view class='item' :class='filterStatus==2 ? "on": ""' @click="clickFilter(2)">
					<view>待回鱼</view>
					<view class='num '>{{totalWaitRetFsh}}</view>
				</view>
				<view class='item' :class='filterStatus==3 ? "on": ""' @click="clickFilter(3)">
					<view>禁用</view>
					<view class='num'>{{totalDisable}}</view>
				</view>
			</view>
		</view>
		

		<view class="main-box">
			<scroll-view class="VerticalNav nav" scroll-y scroll-with-animation :scroll-top="tabScrollPos">
				<view class="cu-item" :class="index==tabCur?'text-green cur':''" v-for="(item,index) in pondPartitions" :key="index" @tap="TabSelect"
				 :data-id="index">
				 <text>{{item.navName}}</text>
				</view>
			</scroll-view>

			<scroll-view class="VerticalMain" scroll-y scroll-with-animation
				:scroll-into-view="'main-'+tabTap" 
				
				@scroll="OnVerticalMainScroll">
				<view v-for="(item,index) in pondPartitions" :key="index" :id="'main-'+index"
				style="background-color: #e8e8e8; margin: 0rpx 20rpx 20rpx 20rpx; padding:0 5rpx; border-radius: 8px;"
				> <!-- #001 -->
					<view style="padding: 20rpx; text-align: center;">
						{{item.navName}}
					</view>
					<view class="SeatBox">
						<view class="Seat" v-for="(seat, seatIdx) in item.seats" :id="seat.id" :key="seatIdx" 
						:style="{
							color: seat.color, 
							backgroundColor: seat.backgroundColor,
							border: seat.border
						}"
						style="margin-bottom: 25upx"
						@click="onTapSeat(index, seatIdx)">
							<text></text>
							<text>{{seat.seatNo}}</text>
							<text v-if="!seat.showCountDown">{{seat.stateTitle}}</text>
							<text class="countdown" v-if="seat.showCountDown"><text style="font-size: 12rpx;">渔:</text>{{seat.retWeight||0}}</text>
							<text class="countdown" v-if="seat.showCountDown">{{seat.countDown.hour||0}}:{{seat.countDown.minute||0}}:{{seat.countDown.second||0}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
		

		<view class="cu-modal" :class="showRetFshModal?'show':''">
			<view class="cu-dialog">
				<view style="display: flex;justify-content: center; align-items: center; margin: 50rpx 0rpx; font-size: large; font-weight: bold;">
					<text>回鱼</text>
				</view>
				<view>
					<view style="height: 30px;width: 10px;;"></view>
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">钓位：</view>
						<view style="width:400upx">{{pondPartitions[currPondPartitionIdx].navName}}/{{pondPartitions[currPondPartitionIdx].seats[currSeatIdx].seatNo}}</view>
					</view>
					
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">条数：</view>
						<input style="width:400upx" type="number" v-model="retNumber" placeholder="请输入回鱼条数"></input>
					</view>
					
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">斤数：</view>
						<input style="width:400upx" type="digit" v-model="retWeight" placeholder="请输入回鱼重量"></input>
					</view>
					
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 50rpx 0rpx;">
						<button style="width: 200rpx; padding: 10rpx 0rpx; background-color: #E93323; color: white;" @click="hideModal">取消</button>
						<button style="width: 200rpx; padding: 10rpx 0rpx; background-color: #E93323; color: white;" @click="doRetFsh">确定</button>
					</view>
				</view>
			</view>
		</view>
		

		<view class="cu-modal" :class="showDisableModal?'show':''">
			<view class="cu-dialog">
				<view style="display: flex;justify-content: center; align-items: center; margin: 50rpx 0rpx; font-size: large; font-weight: bold;">
					<text>{{disableTitle}}</text>
				</view>
				<view>
					<view v-if="dlgBindSeat" style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">IMEI号：</view>
						<input style="width:400upx" v-model="imei" placeholder="请输入设备IMEI号"></input>
					</view>
					
					<block v-else>
						<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
							<view style="width:150upx">钓塘：</view>
							<view style="width:400upx">{{pondPartitions[currPondPartitionIdx].navName}}</view>
						</view>
						
						<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
							<view style="width:150upx">开始钓位：</view>
							<input style="width:400upx" type="number" v-model="disableFromSeatNo" placeholder="请输入开始钓位"></input>
						</view>
						
						<view style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
							<view style="width:150upx">结束钓位：</view>
							<input style="width:400upx" type="number" v-model="disableToSeatNo" placeholder="请输入结束钓位"></input>
						</view>	
					</block>
					
					<view v-if="dlgModifyOd" style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">结束时间：</view>
						<input style="width:400upx" v-model="odEndTime"></input>
					</view>
					
					<view v-if="dlgModifyOd" style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">重新激活：</view>
						<switch :checked="reActivateEndOd" color="#E93323" style="transform:scale(0.8)" @change="onReActivateEndOd" />
					</view>
					
					<view v-if="reActivateEndOd" style="display: flex; justify-content: space-around; align-items: center; margin: 30rpx 0rpx;">
						<view style="width:150upx">停止时间限制：</view>
						<input style="width:400upx" type="number" v-model="reActivateTimeLimit" placeholder="重新激活最近多少分钟内停止的"></input>
					</view>
					
					<view style="display: flex; justify-content: space-around; align-items: center; margin: 50rpx 0rpx;">
						<button style="width: 200rpx; padding: 10rpx 0rpx; background-color: #E93323; color: white;" @click="hideDisable">取消</button>
						<button style="width: 200rpx; padding: 10rpx 0rpx; background-color: #E93323; color: white;" @click="doDisable">确定</button>
					</view>
				</view>
			</view>
		</view>
	
	</view>
</template>

<script>
	import Vue from "vue";
	import {MyTip} from '../../components/ssty/common.js';
	import {
		queryForRetFsh, 
		retFsh,
		disableSeats,
		batchStop,
		batchPause,
		batchReset,
		batchModifyOdEndTime,
		debugDevice,
		setupDevice,
		bindDeviceSeat,
		unbindDeviceSeat
	} from '@/api/fsh.js';

	import shopList from '@/components/shopList';
	import {getDeviceInfo} from '@/api/fsh.js'
	
	export default {
		components: {
		  shopList
		},
		
		
		data() {
			const now = new Date();
			return {
				mer_id: '',
				current: "",
				downStatus: false,
				storeInfo: {},
				filterStatus: -1,
				originalPondPartitions: [],
				pondPartitions: [],
				tabCur: 0,
				tabScrollPos: 0,
				tabTap: -1,
				needCalcPos: true,
				showRetFshModal: false,
				retNumber: null,
				retWeight: null,
				currPondPartitionIdx: undefined,
				currSeatIdx: undefined,
				showDisableModal: false,
				dlgBindSeat: false,
				imei: '',
				dlgModifyOd: false,
				odEndTime: now.getFullYear()+"-"+(now.getMonth()+1)+"-"+(now.getDate()<10?'0'+(now.getDate()):now.getDate())+" "+(now.getHours()<10?('0'+now.getHours()):now.getHours())+":"+(now.getMinutes() < 10?('0'+now.getMinutes()):now.getMinutes()),
				reActivateTimeLimit: '',
				reActivateEndOd: false,
				disableTitle: '批量禁用钓位',
				disableFromSeatNo: undefined,
				disableToSeatNo: undefined,
			};
		},
		watch: {
			pondPartitions: {
				handler(val) {
					this.pondPartitions = val;
				},
				deep: true
			}
		},
		computed: {
			totalPonds() {
				let obj = {};
				return this.originalPondPartitions.reduce((cur, next) => {
					obj[next.pondId] ? "" : (obj[next.pondId]=true && cur.push(next));
					return cur;
				}, []).length;
			},
			totalPartitions() {
				return this.originalPondPartitions.reduce((count, a) => {
					return count + ((a.partitionMode === 1)?1:0);
				}, 0);
			},
			totalSeats() {
				return this.originalPondPartitions.reduce((count, a) => {
					return count + a.seats.length;
				}, 0);
			},
			totalDevices() {
				return this.originalPondPartitions.reduce((count, partition) => {
					let onePartitionDeviceCnt = partition.seats.reduce((cnt, seat)=>{
						return cnt + ((seat.useDevice === 1)?1:0);
					}, 0);
					
					return count + onePartitionDeviceCnt; // 累加分区计数
				}, 0);
			},
			totalUsing() {
				return this.totalSeats - this.totalFree - this.totalDisable;
			}, 
			totalFree() {
				return this.originalPondPartitions.reduce((count, partition) => {
					let onePartitionUsingCnt = partition.seats.reduce((cnt, seat)=>{
						return cnt + ((seat.stateTitle === '空闲' && seat.enable === 1)?1:0);
					}, 0);
					
					return count + onePartitionUsingCnt; // 累加分区计数
				}, 0);
			}, 
			totalWaitRetFsh() {
				return this.originalPondPartitions.reduce((count, partition) => {
					let onePartitionUsingCnt = partition.seats.reduce((cnt, seat)=>{
						return cnt + ((seat.stateTitle === '待回鱼')?1:0);
					}, 0);
					
					return count + onePartitionUsingCnt; // 累加分区计数
				}, 0);
			},
			totalDisable() {
				return this.originalPondPartitions.reduce((count, partition) => {
					let onePartitionUsingCnt = partition.seats.reduce((cnt, seat)=>{
						return cnt + ((seat.enable === 0)?1:0);
					}, 0);
					
					return count + onePartitionUsingCnt; // 累加分区计数
				}, 0);
			}
		},
		onLoad() {
			this.getStoreList();
			this.tmrid = setInterval(()=>this.onTimer(), 1000);
		},
		beforeDestroy() {
			console.log("beforeDestroy, clear tmrid ...");
			if(this.tmrid) {
				clearInterval(this.tmrid);
				delete this.tmrid;
			}
			if(this.autoFreshTmrId) {
				clearInterval(this.autoFreshTmrId);
				delete this.autoFreshTmrId;
			}
		},
		onReady() {
			
		},
		onShow() {
			this.autoFreshTmrId = setInterval(()=> {
				var pages = getCurrentPages();
				let curPage = pages[pages.length - 1];
				// console.log("curPage: ", curPage);
				if(curPage.route == "pages/fsh/retFshManage")
					this.freshPonds();
				// else
				// 	console.log("not retFshManage page, skip interval process")
			}, 5000);
		},
		onHide() {
			if(this.autoFreshTmrId) {
				clearInterval(this.autoFreshTmrId);
				delete this.autoFreshTmrId;
			}
		},
		methods: {
			getStoreList: function() {
			  this.$nextTick(() => {
			    this.$refs.shopList.getStoreList({ customer: 1 })
			  });
			},
			changeStoreClose: function() {
			  this.downStatus = false;
			},
			changeStoreList: function(data) {
			  this.downStatus = !this.downStatus;
			  this.$refs.shopList.isShowStore();
			},
			getStoreInfo: function(data) {
			  let that = this;
			  that.loaded = that.loading = false;
			  that.where = { page: 1, limit: 30 }
			  that.productList = [];
			  that.storeInfo = data;
			  that.mer_id = data.mer_id
			  
			  uni.showLoading({
			  	title: '加载钓位中...',
			  	mask: true
			  });
			  this.freshPonds();
			},
			clickFilter(i) {
				this.filterStatus = i;
				this.doFilter();
			},
			doFilter() {
				//this.originalPondPartitions = res.entity;
				if(-1 == this.filterStatus) {
					this.pondPartitions = this.originalPondPartitions;
					return;
				}
				
				this.pondPartitions = [];
				for(let i = 0; i < this.originalPondPartitions.length; i++) {
					let partition = JSON.parse(JSON.stringify(this.originalPondPartitions[i]));
					switch(this.filterStatus) {
						case 0: // 使用中
							partition.seats = partition.seats.filter(seat=>seat.stateTitle!='空闲');
							break;
						case 1: // 空闲
							partition.seats = partition.seats.filter(seat=>seat.stateTitle=='空闲' && seat.enable === 1);
							break;
						case 2: // 待回鱼
							partition.seats = partition.seats.filter(seat=>seat.stateTitle=='待回鱼');
							break;
						case 3: // 禁用
							partition.seats = partition.seats.filter(seat=>seat.enable === 0);
							break;
					}
					this.pondPartitions.push(partition);
				}
			},
			TabSelect(e) {
				this.tabCur = e.currentTarget.dataset.id;
				this.tabScrollPos = e.currentTarget.dataset.id * 50 // “.VerticalNav.nav .cu-item”中指定了Tab导航栏元素高度为50px
				this.tabTap = this.tabCur; // fix 滚动问题：当滚动到出现一个新的tab时，容易出现回弹
				console.log("retFshManage::TabSelect| id="+e.currentTarget.dataset.id);
			},
			OnVerticalMainScroll(e) {
				let that = this;
				let tabHeight = 0;
				if (this.needCalcPos) {
					console.log("retFshManage::TabSelect| CalcPos");
					this.needCalcPos = false;
					for (let i = 0; i < this.pondPartitions.length; i++) {
						let view = uni.createSelectorQuery().select("#main-" + i);
						view.fields({
							size: true
						}, data => {
							if(!data) {
								this.needCalcPos = true;
								console.log("retFshManage::TabSelect| CalcPos Error, idx="+i);
							}
							else {
								this.pondPartitions[i].top = tabHeight;
								tabHeight = tabHeight + data.height;
								this.pondPartitions[i].bottom = tabHeight;
							}
						}).exec();
					}
					
				}
				
				let scrollTop = e.detail.scrollTop + 10;
				//console.log("retFshManage::OnVerticalMainScroll| scrollTop=%o", scrollTop);
				for (let i = 0; i < this.pondPartitions.length; i++) {
					if (scrollTop > this.pondPartitions[i].top && scrollTop < this.pondPartitions[i].bottom) {
						this.tabScrollPos = i * 50;
						this.tabCur = i; 
						this.tabTap = -1;
						//console.log("retFshManage::OnVerticalMainScroll| tabScrollPos=%o, tabCur=%o", this.tabScrollPos, this.tabCur);
						return false
					}
				}
			},
			
			onTapSeat(pondPartitionIdx, seatIdx) {
				console.log("onTapSeat", pondPartitionIdx, seatIdx);
				this.currPondPartitionIdx = pondPartitionIdx;
				this.currSeatIdx = seatIdx;
				
				let actions = []; // 注意：最多只能6项
				let actions2 = [];
				if(this.pondPartitions[pondPartitionIdx].seats[seatIdx].stateTitle == '待回鱼') {
					actions.push('回鱼');
				}
				
				actions.push('批量停止');
				
				if(getApp().globalData.isPlatformAdmin) {
					let t = this.pondPartitions[pondPartitionIdx].seats[seatIdx];
					
					actions.push(t.useDevice?'取消绑定设备':'绑定设备'); 
					if(t.useDevice)
						actions.push('查看设备IMEI');
						
					actions.push('标定');
					
					if(t.stateTitle == '空闲' && t.useDevice == 1) {
						actions.push('调试启动');
					}
					if(/*t.stateTitle == '垂钓' &&*/ t.useDevice == 1) { // 不管有没有在垂钓，都加个停止菜单，用于处理一些异常情况
						actions.push('调试停止');
					}
				}
				
				if(getApp().globalData.isCmh) {
					actions.push('modifyOdOrReactivate');
					actions.push('show-device-log');
				}
				
				actions.push('查看鱼票和回鱼记录');
				//actions.push('批量暂停垂钓');
				//actions.push('批量恢复启动垂钓');
				actions.push(this.pondPartitions[pondPartitionIdx].seats[seatIdx].enable?'批量禁用钓位':'批量启用钓位');
				// actions.push('批量禁用钓位');
				// actions.push('批量启用钓位');
				
				actions.push('重置钓位订单');
				actions.push('切换服务器');
				if(actions.length > 6) { // 注意：最多只能6项
					actions2 = actions.slice(5);
					actions = actions.slice(0, 5);
					actions.push('更多...');
				}
				
				this.showActions(pondPartitionIdx, seatIdx, actions, actions2)
			},
			
			showActions(pondPartitionIdx, seatIdx, actions, actions2) {
				let that = this;
				uni.showActionSheet({
				    itemList: actions,
				    success: function (res) {
				        //console.log('选中了第' + (res.tapIndex + 1) + '个按钮');
						switch(actions[res.tapIndex])
						{
							case '回鱼': {
								that.showRetFshModal = true;
								that.retNumber = null;
								that.retWeight = null;
								if(that.pondPartitions[pondPartitionIdx].seats[seatIdx].stateTitle == '待回鱼') {
									that.showRetFshModal = true;
								}
								break;
							}
							case '查看鱼票和回鱼记录': {
								uni.navigateTo({
									url:'/pages/fsh/fshOrders?pageType=3&seatId='+that.pondPartitions[pondPartitionIdx].seats[seatIdx].id
								})
								break;
							}
							case '查看设备IMEI': {
								getDeviceInfo(that.pondPartitions[pondPartitionIdx].seats[seatIdx].deviceSn).then(res=>{
									uni.showModal({
										title: that.pondPartitions[pondPartitionIdx].seats[seatIdx].deviceSn,
										content: "硬件版本："+res.entity.hvar+"，协议版本："+res.entity.pvar,
										showCancel: false
									});
								}).catch(err => {
									uni.showToast({
										title: err.msg
									})
									uni.hideLoading();
								});
								break;
							}
							case '标定': {
								let deviceSn = that.pondPartitions[pondPartitionIdx].seats[seatIdx].deviceSn;
								setupDevice("weightCalibration", [{deviceSn}]).then(res=>{
									uni.showToast({
										title: '标定完成'
									})
								}).catch(err => {
									uni.showToast({
										title: err.msg
									})
									uni.hideLoading();
								});
								break;
							}
							case '切换服务器':{
								console.log("ip");
								that.disableFromSeatNo = that.disableToSeatNo = that.pondPartitions[that.currPondPartitionIdx].seats[seatIdx].seatNo;
								that.showDisableModal = true;
								that.disableTitle="切换服务器";
								
								
								break;
							}
							// ====== begin 禁用钓位&&暂停&&启动功能 ======
							
							case '批量暂停垂钓':
							case '批量恢复启动垂钓':
							case '批量禁用钓位': 
							case '批量启用钓位':
							case '绑定设备':
							case '调试启动':
							case '调试停止': 
							case '批量停止':
							case '重置钓位订单':
							case 'modifyOdOrReactivate': {
								that.showDisableModal = true;
								that.dlgBindSeat = '绑定设备' == actions[res.tapIndex];
								that.dlgModifyOd = 'modifyOdOrReactivate' == actions[res.tapIndex];
								that.disableTitle = actions[res.tapIndex];
								that.disableFromSeatNo = that.disableToSeatNo = that.pondPartitions[pondPartitionIdx].seats[seatIdx].seatNo;
								that.disablePartitionId = that.pondPartitions[pondPartitionIdx].partitionId;
								that.disablePondId = that.pondPartitions[pondPartitionIdx].pondId;
								break;
							}
							// ====== end 禁用钓位&&暂停&&启动功能 ======
							case '取消绑定设备': {
								uni.showModal({
									title: '提示',
									content: '确认取消绑定？',
									success: function(res) {
										if (res.confirm) {
											unbindDeviceSeat(that.pondPartitions[pondPartitionIdx].seats[seatIdx].deviceSn).then(res => {
												this.hideDisable();
												setTimeout(()=>{
													uni.showToast({
														title: '取消绑定完成'
													})
													this.freshPonds();
												}, 1000);
											}).catch(err => {
												uni.showToast({
													title: err.msg
												})
												uni.hideLoading();
											});
										}
									}
								});
								break;
							}
							case 'show-device-log': {
								uni.navigateTo({
									url:'/pages/fsh/debug?type=deviceLog&deviceSn='+that.pondPartitions[pondPartitionIdx].seats[seatIdx].deviceSn
								})
								break;
							}
							case '更多...': {
								that.showActions(pondPartitionIdx, seatIdx, actions2)
								break;
							}
						}
				    },
				    fail: function (res) {}
				});
			},
			
			onTimer() {
				for(let i = 0; i < this.pondPartitions.length; i++) {
					let partition = this.pondPartitions[i];
					for(let j = 0; j < partition.seats.length; j++) {
						let seat = partition.seats[j]; 
						if(seat.showCountDown) {
							if(0 === seat.countDown.type) { // 0-倒计时，1-正计时
								let {hour, minute, second, type} = seat.countDown;
								if(second > 0)
									--second;
								else {
									if(minute > 0) {
										--minute;
										second=59;
									}
									else {
										if(hour > 0) {
											--hour;
											second=59;
											minute=59;
										}
										else {
											second=0;
											minute=0;
											hour = 0;
											
											clearTimeout(this.__freshPondsTmrId);
											this.__freshPondsTmrId = setTimeout(()=>{
												this.freshPonds();
											}, 3000);
										}
									}
								}
								Vue.set(this.pondPartitions[i].seats[j], 'countDown', {hour, minute, second, type}); // 为什么用Vue.set: 《Vue.set 可以设置实例创建之后添加的属性，而this.$set只能设置实例创建后存在的属性 https://www.cnblogs.com/xuran1216/p/13182428.html》
							}
							else {
								let {hour, minute, second, type} = seat.countDown;
								++second;
								if(second >= 60) {
									second = 0;
									++minute;
									if(minute >= 60) {
										minute = 0;
										++hour;
									}
								}
								Vue.set(this.pondPartitions[i].seats[j], 'countDown', {hour, minute, second, type}); // 为什么用Vue.set: 《Vue.set 可以设置实例创建之后添加的属性，而this.$set只能设置实例创建后存在的属性 https://www.cnblogs.com/xuran1216/p/13182428.html》
							}
						}
					}
				}
			},
			doRetFsh() {
				if(this.retNumber == null) {
					MyTip("请输入数量", 800);
					return;
				}
				
				if(this.retWeight == null) {
					MyTip("请输入斤数", 800);
					return;
				}
					
				uni.showLoading({
					title: '回鱼中...',
					mask: true
				});
				
				let odId = this.pondPartitions[this.currPondPartitionIdx].seats[this.currSeatIdx].odId;
				retFsh(odId, this.retNumber, this.retWeight, 0, "").then(res => {
					this.showRetFshModal = false;
					this.freshPonds();
					MyTip("回鱼成功");
				}).catch(err => {
					uni.hideLoading();
					MyTip("回鱼失败："+err);
					this.showRetFshModal = false;
				});
				
			},
			
			hideModal() {
				this.showRetFshModal = false;
			},
			
			freshPonds() {
				console.log("retFshManage::freshPonds| queryForRetFsh ...");
				queryForRetFsh(this.mer_id).then(res => {
					uni.hideLoading();
					this.originalPondPartitions = res.entity;
					this.doFilter();
					this.needCalcPos = true;
					console.log("retFshManage::freshPonds| queryForRetFsh ok, mer_id=%o, pondPartitions=%o", this.mer_id, res.entity);
				}).catch(err => {
					console.log("queryForRetFsh fail:", err);
					uni.hideLoading();
				});
			},

			hideDisable() {
				this.showDisableModal = false;
			},
			doDisable() {
				uni.showLoading({
					title: '请求中...',
					mask: true
				});
				if(this.disableTitle=='调试启动' || this.disableTitle=='调试停止') {
					let seatIdAry = []
					let firstSeatNoOfPartiton = this.pondPartitions[this.currPondPartitionIdx].seats[0].seatNo;
					for(let i = (this.disableFromSeatNo - firstSeatNoOfPartiton); i <= (this.disableToSeatNo - firstSeatNoOfPartiton); i++) {
						seatIdAry.push(this.pondPartitions[this.currPondPartitionIdx].seats[i].id);
					}
					let seatIds = seatIdAry.join(",");
					debugDevice(this.disablePondId, this.disablePartitionId, seatIds, this.disableTitle=='调试启动' ? 'debugStart' : 'debugStop').then(res => {
						setTimeout(()=>{
							uni.hideLoading();
							this.hideDisable();
							uni.showToast({
								title: this.disableTitle+'成功'
							})
							this.freshPonds();
						}, 6000);
					}).catch(err => {
						uni.hideLoading();
						uni.showToast({
							title: err.msg
						})
						
					});
				}
				else if(this.disableTitle=='绑定设备') {
					let seat = this.pondPartitions[this.currPondPartitionIdx].seats[this.currSeatIdx];
					bindDeviceSeat(this.imei, seat.id, seat.seatNo).then(res => {
						this.hideDisable();
						setTimeout(()=>{
							uni.showToast({
								title: '绑定完成'
							})
							this.freshPonds();
						}, 1000);
					}).catch(err => {
						uni.showToast({
							title: err.msg
						})
						uni.hideLoading();
					});
				}
				else if(this.disableTitle=='批量禁用钓位' || this.disableTitle=='批量启用钓位') {
					disableSeats(this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo, this.disableTitle=='批量禁用钓位').then(res => {
						let num = Math.abs(res.entity);
						this.hideDisable();
						uni.hideLoading();
						uni.showToast({
							title: this.disableTitle=='批量禁用钓位'?('禁用成功数量:'+num):('启用成功数量:'+num)
						})
						this.freshPonds();
					}).catch(err => {
						uni.hideLoading();
					});
				}
				else if(this.disableTitle=='批量停止') {
					console.log("批量停止", this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo);
					batchStop(this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo).then(res=>{
						let num = Math.abs(res.entity);
						uni.showToast({
							title: '实际停止数量:'+num
						})
						uni.hideLoading();
						this.freshPonds();
					}).catch(err => {
						uni.hideLoading();
						uni.showToast({
							title: '批量停止失败'
						})
					});
					this.hideDisable();
				}
				else if(this.disableTitle=='重置钓位订单') {
					console.log("重置钓位订单", this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo);
					batchReset(this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo).then(res=>{
						console.log(res)
						let num = Math.abs(res.entity);
						uni.showToast({
							title: '实际重置数量:'+num
						})
						uni.hideLoading();
						this.freshPonds();
					}).catch(err => {
						console.log(res)
						uni.hideLoading();
						uni.showToast({
							title: '批量重置失败'
						})
					});
					this.hideDisable();
				}
				else if(this.disableTitle=='modifyOdOrReactivate') {
					console.log("modifyOdOrReactivate", this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo, this.odEndTime, this.reActivateEndOd, this.reActivateTimeLimit);
					batchModifyOdEndTime(this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo, this.odEndTime, this.reActivateEndOd, this.reActivateTimeLimit).then(res=>{
						let num = Math.abs(res.entity);
						uni.showToast({
							title: '实际修改数量:'+num
						})
						uni.hideLoading();
						this.freshPonds();
					}).catch(err => {
						uni.hideLoading();
						uni.showToast({
							title: '批量修改失败'
						})
					});
					this.hideDisable();
				}
				else if(this.disableTitle=='切换服务器') {
					let that=this;
					// deviceSn: a.deviceSn, 
					// serverIP:res.tapIndex==0?'device.zhidiaotianxia.com':'yxdev.chuidiaotop.com',
					// serverPORT:res.tapIndex==0?8090:8001
					let starIndex=Number(that.disableFromSeatNo);
					let endIndex=Number(that.disableToSeatNo);
					let arraySeats=that.pondPartitions[that.currPondPartitionIdx].seats;
					let devices=[],devicesTwo=[];
					for(let i=starIndex;i<=endIndex;i++){
						devices.push(arraySeats[i-1]);
						console.log(arraySeats[i-1],i-1);
					}
					devicesTwo=devices.map((item)=>{
						let obj={};
						obj.deviceSn=item.deviceSn;
						obj.serverIP="device.zhidiaotianxia.com";
						obj.serverPORT="8090";
						return obj
					});
					console.log(this.disableFromSeatNo,this.disableToSeatNo,starIndex,endIndex,devicesTwo,devices);
					setupDevice("setServerAddr",devicesTwo).then(res=>{
						let num = Math.abs(res.entity);
						uni.showToast({
							title: '切换成功'
						})
						uni.hideLoading();
						this.freshPonds();
					}).catch(err => {
						uni.hideLoading();
						uni.showToast({
							title: '切换失败'
						})
					});
					this.hideDisable();
				}
				else {
					batchPause(this.disablePondId, this.disablePartitionId, this.disableFromSeatNo, this.disableToSeatNo, this.disableTitle=='批量暂停垂钓').then(res => {
						let num = Math.abs(res.entity);
						this.hideDisable();
						uni.hideLoading();
						uni.showToast({
							title: this.disableTitle=='批量暂停垂钓'?('暂停成功数量:'+num):('启动成功数量:'+num)
						})
						this.freshPonds();
					}).catch(err => {
						uni.hideLoading();
					});
				}
			},
			onReActivateEndOd(e) {
				this.reActivateEndOd = e.detail.value ? 1: 0;
			},
		}
	}
</script>

<style lang="scss" scoped>
	$topHeight: 280rpx;
	.top-box {
		height: $topHeight;
	}
	
	.top-box .header {
		height: 220rpx;
		padding: 0 30rpx;
		background-color: #e93323;
	}
	
	.top-box .header .picTxt {
		height: 210rpx;
	}
	
	.top-box .header .picTxt .text {
		color: rgba(255, 255, 255, 0.8);
		font-size: 26rpx;
		// font-family: 'Guildford Pro';
	}
	
	.top-box .header .picTxt .text .name {
		font-size: 34rpx;
		font-weight: bold;
		color: #fff;
		maring-top: -10rpx;
		margin-bottom: 10rpx;
		text-align: center;
	}
	
	.top-box .header .picTxt .pictrue {
		width: 122rpx;
		height: 109rpx;
	}
	
	.top-box .header .picTxt .pictrue image {
		width: 100%;
		height: 100%;
	}
	
	.top-box .filter {
		background-color: #fff;
		width: 650rpx;
		height: 110rpx;
		border-radius: 6rpx;
		margin: -70rpx auto 0 auto;
	}
	
	.top-box .filter .item {
		text-align: center;
		font-size: 26rpx;
		color: #282828;
		padding: 15rpx 0;
		min-width: 100rpx;
	}
	
	.top-box .filter .item.on {
		font-weight: bold;
		border-bottom: 5rpx solid #e93323;
	}
	
	.top-box .filter .item .num {
		margin-top: 18rpx;
	}

	.main-box {
		display: flex;
	}
	
	.VerticalNav.nav {
		width: 200upx;
		white-space: initial;
		height:calc(100vh - #{$topHeight});
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.VerticalNav.nav .cu-item.cur {
		background-color: #f7f7f7;
		border-left: 4rpx solid #fc4141;
		width: 100%;
		text-align: center;
		color: #fc4141;
		font-weight: bold;
	}

	.VerticalMain {
		background-color: #f7f7f7;
		flex: 1;
		height:calc(100vh - #{$topHeight});
	}
	
	.SeatBox {
		display: flex; 
		flex-wrap: wrap; 
		/*justify-content: space-around;*/
		align-items: center;  
		align-content: center;
		// justify-content: space-around;
	}
	.Seat {
		width: 105upx; 
		height: 105upx;
		background-color: #FFFFFF;
		border-radius: 10upx;
		margin-left: 20upx;
		
		display: flex;
		justify-content: space-around;
		align-items: center;  
		flex-direction: column;
		
		.countdown {
			font-size: x-small;
		}
	}

	.popupn {
	  position: fixed;
	  width: 100%;
	  text-align: center;
	  top: 0;
	  left: 0;
	  background: transparent;
	  height: 90rpx;
	  line-height: 90rpx;
	  z-index: 5;
	  .spin {
	    display: block;
	    transform: rotate(180deg);
	    font-size: 36rpx;
	  }
	  .title {
	    max-width: 560rpx;
	    margin: 0 auto;
	    position: relative;
	    display: inline;
	    padding: 10rpx 30rpx 10rpx 50rpx;
	    background-color: rgba(0,0,0,.25);
	    border-radius: 30rpx;
	    color: #FFFFFF;
	  }
	  .iconfont {
	    display: inline-block;
	    position: relative;
	    top: 4rpx;
	    right: 0;
	  }
	  .mer_logo {
	    width: 34rpx;
	    height: 34rpx;
	    position: relative;
	    top: 6rpx;
	    right: 10px;
	  }
	  .mer_name {
	    display: inline-block;
	    max-width: 650rpx;
	  }
	  .invoice-content {
	    background-color: #ffffff;
	  }
	}
	
</style>
